package vn.vietel.pms.web.action;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.faces.convert.Converter;
import javax.faces.event.ActionEvent;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

import vn.vietel.pms.converter.GroupTypeConverter;
import vn.vietel.pms.converter.KindTypeConverter;
import vn.vietel.pms.converter.TypeTypeConverter;
import vn.vietel.pms.entity.VGroupOfProperty;
import vn.vietel.pms.entity.VKindOfProperty;
import vn.vietel.pms.entity.VListOfProperty;
import vn.vietel.pms.entity.VTypeOfProperty;
import vn.vietel.pms.service.GroupOfPropertyService;
import vn.vietel.pms.service.KindOfPropertyService;
import vn.vietel.pms.service.ListPropertyService;
import vn.vietel.pms.service.TypeOfPropertyService;

@Scope(ScopeType.SESSION)
@Name("listOfPropertyAction")
public class ListOfPropertyAction {
	@In(required = false)
	@Out(required = false)
	@DataModelSelection("listPropertyItems")
	private VListOfProperty vlistofproperty;

	@Logger
	Log log;
	@In("#{listOfPropertyService}")
	private ListPropertyService listOfPropertyService;

	@In("#{groupOfPropertyService}")
	private GroupOfPropertyService groupOfPropertyService;

	@In("#{kindOfPropertyService}")
	private KindOfPropertyService kindOfPropertyService;

	@In("#{typeOfPropertyService}")
	private TypeOfPropertyService typeOfPropertyService;

	@In
	private FacesMessages facesMessages;

	@DataModel
	private List<VListOfProperty> listPropertyItems;

	private Map<String, VGroupOfProperty> groupList;

	private Map<String, VKindOfProperty> kindList;

	private Map<String, VTypeOfProperty> typeList;

	private List<VGroupOfProperty> groups;

	private List<VKindOfProperty> kinds;

	private List<VTypeOfProperty> types;

	private Long id;

	private boolean editCompleted;

	private boolean editting;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
		editCompleted = false;
		editting = false;
		if (id != null) {
			vlistofproperty = listOfPropertyService.find(id);
			editting = (vlistofproperty != null);
		} else {
			vlistofproperty = new VListOfProperty();
			editting = false;
		}
	}

	public void createItem() {
		try {
			editCompleted = false;
			VGroupOfProperty g = groupOfPropertyService
					.findGroup(vlistofproperty.getGroupOfDevice().getId());
			vlistofproperty.setGroupOfDevice(g);

			VTypeOfProperty t = typeOfPropertyService.find(vlistofproperty
					.getTypeOfDevice().getId());
			vlistofproperty.setTypeOfDevice(t);

			VKindOfProperty k = kindOfPropertyService.find(vlistofproperty
					.getKindOfDevice().getId());
			vlistofproperty.setKindOfDevice(k);

			listOfPropertyService.create(vlistofproperty);
			getListPropertyItems();
			editCompleted = true;
		} catch (IllegalStateException e) {
			facesMessages.add("Opps!!! Could not insert....");
			facesMessages.add(e.getMessage());
			editCompleted = false;
		}
	}

	public void updateItem() {
		try {
			editCompleted = false;
			listOfPropertyService.update(vlistofproperty);
			getListPropertyItems();
			editCompleted = true;
			editting = false;

		} catch (IllegalStateException e) {
			facesMessages.add(e.getMessage());
		}
	}

	public void deleteItem(ActionEvent ae) {
		try {
			Long rId = vlistofproperty.getId();
			VListOfProperty rgp = listOfPropertyService.find(rId);
			listOfPropertyService.delete(rgp);
			getListPropertyItems();
		} catch (IllegalStateException e) {
			facesMessages.add(e.getMessage());
		}
	}

	@Factory()
	public List<VListOfProperty> getListPropertyItems() {
		listPropertyItems = listOfPropertyService.getAll();
		getGroups();
		getKinds();
		getTypes();
		if (listPropertyItems == null)
			listPropertyItems = new LinkedList<VListOfProperty>();
		return listPropertyItems;
	}

	public void setlistPropertyItems(List<VListOfProperty> items) {
		this.listPropertyItems = items;
	}

	public boolean isEditCompleted() {
		return editCompleted;
	}

	public void setEditCompleted(boolean editcompleted) {
		this.editCompleted = editcompleted;
	}

	public boolean isEditting() {
		return editting;
	}

	public void setEditting(boolean editting) {
		this.editting = editting;
	}

	public boolean notEditing() {
		return !editting;
	}

	@Factory("groupList")
	public Map<String, VGroupOfProperty> getGroupList() {
		return groupList;
	}

	public void setGroupList(Map<String, VGroupOfProperty> groupList) {
		this.groupList = groupList;
	}

	public Map<String, VKindOfProperty> getKindList() {
		return kindList;
	}

	public void setKindList(Map<String, VKindOfProperty> kindList) {
		this.kindList = kindList;
	}

	public Map<String, VTypeOfProperty> getTypeList() {
		return typeList;
	}

	public void setTypeList(Map<String, VTypeOfProperty> typeList) {
		this.typeList = typeList;
	}

	public Converter getGroupConverter() {
		return new GroupTypeConverter(groups);
	}

	public Converter getTypeConverter() {
		return new TypeTypeConverter(types);
	}

	public Converter getKindConverter() {
		return new KindTypeConverter(kinds);
	}

	public List<VGroupOfProperty> getGroups() {
		groups = listOfPropertyService.getAllGroups();
		groupList = new TreeMap<String, VGroupOfProperty>();
		for (VGroupOfProperty item : groups) {
			groupList.put(item.getGroupName(), item);
		}
		return groups;
	}

	public void setGroups(List<VGroupOfProperty> groups) {
		this.groups = groups;
	}

	@Factory("kinds")
	public List<VKindOfProperty> getKinds() {
		kinds = kindOfPropertyService.getAll();
		kindList = new TreeMap<String, VKindOfProperty>();
		for (VKindOfProperty item : kinds) {
			kindList.put(item.getKindName(), item);
		}
		return kinds;
	}

	public void setKinds(List<VKindOfProperty> kinds) {
		this.kinds = kinds;
	}

	@Factory("types")
	public List<VTypeOfProperty> getTypes() {
		types = typeOfPropertyService.getAll();
		typeList = new TreeMap<String, VTypeOfProperty>();
		for (VTypeOfProperty item : types) {
			typeList.put(item.getTypeName(), item);
		}
		return types;
	}

	public void setTypes(List<VTypeOfProperty> types) {
		this.types = types;
	}

}
